package ontocompo.ui.screenmanagement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;


public class ScreenPossibilities extends JPanel implements MouseListener, ActionListener {
	
	protected final static int W = 100;
	protected final static int E = 25;
	protected final static int H = 80;
	protected Dimension d = new Dimension(700, H);
	protected String buttonLbl = "extract to";
	
	public void setButtonMessage(String buttonLbl) {
		this.buttonLbl = buttonLbl;
	}

	public class ScreenRepresentation extends JPanel {
		
		Screen myscreen;
		private boolean enableExtraction;
		Dimension d = new Dimension(W, H);
		
		public Screen getScreen()
		{
			return myscreen;
		}
		
		ScreenRepresentation(Screen s, boolean enableExtraction)
		{
			myscreen = s;
			
			setLayout(null);
			setSize(d);
			setPreferredSize(d);
			setMaximumSize(d);
			setMinimumSize(d);
			
			if (enableExtraction)
			{
				JButton extractTo = new JButton(ScreenPossibilities.this.buttonLbl);
				extractTo.setSize(d.width,d.height/4);
				extractTo.setLocation(0, 0);
				extractTo.setBorder(BorderFactory.createEtchedBorder(Color.darkGray, Color.lightGray));

				add(extractTo);
				
				extractTo.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						ScreenPossibilities.this.extraction.consume(myscreen);
						setBackground(Color.yellow);
						if (ScreenPossibilities.this.selected != null) 
							{
							ScreenPossibilities.this.selected.setBackground(Color.white); 
							}
						ScreenPossibilities.this.selected = ScreenRepresentation.this;
					}
				});
			}
			
			JLabel info = new JLabel(s.getName(), JLabel.CENTER);
			info.setSize(d.width,(d.height*3)/4);
			info.setLocation(0,d.height/4);
			add(info);
			
			setBackground(Color.white);
			setBorder(BorderFactory.createEtchedBorder(Color.darkGray, Color.lightGray));
		}

		
		
		
	}
	
	// private ScreenManager manager;
	protected  ArrayList<ScreenRepresentation> screens ; // = new ArrayList<ScreenRepresentation>();
	protected  boolean extract;
	protected  JPopupMenu popup;
	protected JButton gauche, droite;
	protected JPanel centre ;
	protected  boolean addscreen;
	protected  ActionListener creation;
	protected  ScreenConsumer extraction;
	protected ScreenRepresentation selected;
	
	public ScreenPossibilities( boolean addscreen,  ActionListener creation, boolean extract, ScreenConsumer extraction)
	{
		this.addscreen = addscreen;
		this.creation = creation ;
		// this.manager = screenManager;
		this.extract = extract;
		this.extraction = extraction;
	}
	
	public void init()
	{
		setLayout(new BorderLayout());
		setSize(d);
		setPreferredSize(d);
		setMaximumSize(d);
		setMinimumSize(d);
		
		ImageIcon left = new ImageIcon("fleches/basic/arrow00.png");
		ImageIcon right = new ImageIcon("fleches/basic/arrow04.png");
		final Color transColor = new Color(0,0,0,0);
	
		gauche = new JButton(left);
		gauche.setActionCommand("gauche");
		gauche.addActionListener(this);
		gauche.setOpaque(false);
		gauche.setBorderPainted(false);
		gauche.setBackground(transColor);
		gauche.setFocusable(false);
		add(gauche, "West");
		
		droite = new JButton(right);
		droite.setActionCommand("droite");
		droite.addActionListener(this);
		droite.setOpaque(false);
		droite.setBackground(transColor);
		droite.setBorderPainted(false);
		droite.setFocusable(false);
		add(droite, "East");
		
		centre = new JPanel(null);
		add(centre);
		
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		
		if (addscreen)
		{
			/**
			 * ajout d'�cran par popup...
			 */
			JMenuItem jmi = new JMenuItem("Add New Screen");
			jmi.addActionListener(creation);
			popup = new JPopupMenu();
			popup.add(jmi);
			this.addMouseListener(this);
		}
		
		/**
		 * init des ecrans
		 */
		// updateScreensList(manager.getAvailabledScreens());
	}
	
	public void updateScreensList(ArrayList<Screen> screens) {
		centre.removeAll();
		this.screens = new ArrayList<ScreenRepresentation>();
		int x = 0, y = 0;
		for(Screen s: screens) {
			ScreenRepresentation r = new ScreenRepresentation(s, extract);
			r.setLocation(x,y);
			x = x+W+E;
			this.screens.add(r);
			centre.add(r);
		}
		
		repaint();
	}


	
	
	@Override
	public void actionPerformed(ActionEvent a) {
		if ((screens == null) || (screens.size() <= 1)) return; 
		String cmd = a.getActionCommand();
		int delta = 0;
		
		
		
		if (cmd.equals("gauche"))
		{
			delta = -(W+E); 
		}
		else if (cmd.equals("droite"))
		{
			delta = E+W;
		}
		
		
		
		if (delta != 0)
		{
			int modulo = screens.size()*(E+W);
			// for(ScreenRepresentation r : screens)
			for(Component r : centre.getComponents())
			{
				// System.out.println(r.myscreen.getName()+ " : "+r.getLocation());
				int x = r.getX();
				x = x + delta;
				if (x < 0) x = modulo + x;
				if (x >= modulo) x = x - modulo;
				r.setLocation(x, r.getY());
				centre.validate();
			}
		}
		
		
		repaint();
		
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger()) {
            popup.show(e.getComponent(),
                       e.getX(), e.getY());
        }
		
	}



	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger()) {
            popup.show(e.getComponent(),
                       e.getX(), e.getY());
        }
	}
	
	
}
